import { EditorFile, MainUI } from "./MainUI.js";
import { tt } from "../ttapi_interface/ttapi.js";
import * as tt2 from "../ttapi_layer2/ttlayer2.js";
import * as EUI from "./eui/EUI.js"
import { EditorManager } from "./fileeditor/editor.js";

export class MainUI_FileTab {


    static _InitFileTab(mainui: MainUI, container: tt2.QUI_IElement) {
        mainui.uiFileTab = new EUI.EUI_TabBar();
        mainui.uiFileTab.localRect.setHPosFill();
        mainui.uiFileTab.localRect.setVPosByTopBorder(18);
        container.addChild(mainui.uiFileTab)

        let defitem = mainui.uiFileTab.AddItem("Default", false);
        mainui.uiFileTab.OnSelect = (name, index) => {
            if (index == 0) {
                mainui.uiEditArea.removeChildAll();
                if (mainui.tempeditor.ui != null) {

                    mainui.uiEditArea.addChild(mainui.tempeditor.ui);
                }
                mainui.textEditor.text = mainui.tempeditor.info;
                mainui.btnEditor.Enable = mainui.tempeditor.canopen;
                EUI.EUI_Style.UpdateSubLabel(mainui.btnEditor, "打开");
            }
            else {
                let editor = (mainui.uiFileTab.select as any)._editor as EditorFile;
                mainui.uiEditArea.removeChildAll();
                mainui.uiEditArea.addChild(editor.ui);
                mainui.textEditor.text = editor.info;
                mainui.btnEditor.Enable = editor.canopen;
                EUI.EUI_Style.UpdateSubLabel(mainui.btnEditor, "保存");
            }
        };


        {//header

            let header = EUI.EUI_Style.CreateDefaultBack();
            header.localRect.setHPosFill();
            header.localRect.setVPosByTopBorder(18, 18);
            container.addChild(header);

            let x = 0;

            {
                let btn = mainui.btnEditor = EUI.EUI_Style.CreateButton("打开");
                btn.localRect.setByRect(new tt.Rectangle(x, 2, 40, 14));
                x += 40;
                header.addChild(btn);
                btn.OnClick = async () => {
                    //如果当前选中的是第0个，那就是预览部分
                    if (mainui.uiFileTab.select == null)
                        return;
                    if (mainui.uiFileTab.select.index == 0) {
                        if (mainui.tempeditor.editor != null) {
                            let e = mainui.tempeditor.Clone();
                            let filename = e.editor.getFilename();
                            let name = tt.PathTool.GetFileName(tt.PathTool.CheckPath(filename));
                            mainui.alleditor[e.editor.getFilename()] = e;
                            let tabitem = mainui.uiFileTab.AddItem(name, true, async (_f) => {
                                if (e.editor.IsChange()) {
                                    let msg = new EUI.EUI_Dialog_Msgbox();
                                    let ret = await msg.Show(mainui.window, "还没保存,是否强行关闭", true);
                                    if (ret == false)
                                        return true;
                                    return false;
                                }
                                let filename = e.editor.getFilename();
                                delete mainui.alleditor[filename];
                                await e.editor.OnExitAsync();
                                mainui.uiFileTab.SelectItemAt(0);
                               
                                return false;
                            });
                            e.baritem = tabitem;
                            let editor = e.editor;

                            mainui.tempeditor.Clear();
                            mainui.tempeditor.info = "空";
                            mainui.tempeditor.canopen = false;

                            mainui.uiFileTab.items[0].name = "<NULL>";
                            (tabitem as any)._editor = e;
                            await editor.Open()
                            mainui.uiFileTab.SelectItemAt(tabitem.index);
                            //正式编辑
                        }
                    }
                    else {
                        //如果选中了其它文件，则就是个保存按钮
                        let editor = (mainui.uiFileTab.select as any)._editor as EditorFile;
                        let b = await editor.editor.Save();
                        if (b) {
                            editor.baritem.modify = false;
                            mainui.uiFileTab.UpdateItemUIAt(editor.baritem.index);
                            //还需要刷新 tabitem
                        }

                    }
                }
            }
            {
                let label = mainui.textEditor = new tt2.QUI_Label(EUI.EUI_Style.font, "");
                label.halign = tt2.QUI_HAlign.Left;
                label.localRect.setByRect(new tt.Rectangle(x, 2, 200, 14));
                x += 200;
                header.addChild(label);


            }
        }


        mainui.uiEditArea = new tt2.QUI_Container();
        mainui.uiEditArea.localRect.setHPosFill();
        mainui.uiEditArea.localRect.setVPosFill(36, 0);
        container.addChild(mainui.uiEditArea)


        mainui.uiFileTab.SelectItemAt(0);
    }

}
